package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.bpodgursky.jbool_expressions.Expression;
import com.ruoyi.system.domain.Drug;
import com.ruoyi.system.domain.DrugRule;
import com.ruoyi.system.domain.vo.VerifyVo;
import com.ruoyi.system.service.IDrugRuleService;
import com.ruoyi.system.service.IDrugService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.system.domain.DrugVerify;
import com.ruoyi.system.service.IDrugVerifyService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 药品审核Controller
 * 
 * @author wmb
 * @date 2020-08-27
 */
@RestController
@RequestMapping("/drugverify")
public class DrugVerifyController extends BaseController
{
    @Autowired
    private IDrugVerifyService drugVerifyService;

    @Autowired
    private IDrugRuleService drugRuleService;

    @Autowired
    private IDrugService drugService;
    /**
     * 查询药品审核列表
     */
    @PreAuthorize("@ss.hasPermi('system:drugverify:list')")
    @GetMapping("/list")
    public TableDataInfo list(DrugVerify drugVerify)
    {
        startPage();
        List<DrugVerify> list = drugVerifyService.selectDrugVerifyList(drugVerify);
        return getDataTable(list);
    }

    /**
     * 导出药品审核列表
     */
    @PreAuthorize("@ss.hasPermi('system:drugverify:export')")
    @Log(title = "药品审核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DrugVerify drugVerify) throws IOException
    {
        List<DrugVerify> list = drugVerifyService.selectDrugVerifyList(drugVerify);
        ExcelUtil<DrugVerify> util = new ExcelUtil<DrugVerify>(DrugVerify.class);
        util.exportExcel(response, list, "drugverify");
    }

    /**
     * 获取药品审核详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:drugverify:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(drugVerifyService.selectDrugVerifyById(id));
    }

    /**
     * 新增药品审核
     */
    @PreAuthorize("@ss.hasPermi('system:drugverify:add')")
    @Log(title = "药品审核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DrugVerify drugVerify)
    {
        return toAjax(drugVerifyService.insertDrugVerify(drugVerify));
    }

    /**
     * 修改药品审核
     */
    @PreAuthorize("@ss.hasPermi('system:drugverify:edit')")
    @Log(title = "药品审核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DrugVerify drugVerify)
    {
        return toAjax(drugVerifyService.updateDrugVerify(drugVerify));
    }

    /**
     * 删除药品审核
     */
    @PreAuthorize("@ss.hasPermi('system:drugverify:remove')")
    @Log(title = "药品审核", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(drugVerifyService.deleteDrugVerifyByIds(ids));
    }



    @PostMapping("/verify")
    public AjaxResult verify(@RequestBody VerifyVo verify)
    {

        Drug drug = drugService.selectDrugByName(verify.getDrugName());

        DrugRule rule = drugRuleService.selectDrugRuleByDrugId(drug.getId());
        verify.setDrugRuleId(rule.getId());
        Boolean expression = (Boolean) drugRuleService.verify(verify).get("result");

        return AjaxResult.success(expression);
    }


    @GetMapping(value = "/dailyStat")
    public AjaxResult dailyStat()
    {
        return AjaxResult.success(drugVerifyService.dailyStat(null));
    }



    @PostMapping("/verifyDetail")
    public AjaxResult verifyDetail(@RequestBody VerifyVo verify)
    {

        Drug drug = drugService.selectDrugByName(verify.getDrugName());

        DrugRule rule = drugRuleService.selectDrugRuleByDrugId(drug.getId());
        verify.setDrugRuleId(rule.getId());
        DrugRule ruleDetail = drugRuleService.verifyDetail(verify);

        return AjaxResult.success(ruleDetail);
    }
}
